package com.zyx.jdbcdemo.hikari;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Yaxi.Zhang
 * @since 2022/3/30 11:11
 * desc: 使用配置文件配置Hikari参数
 * https://gitee.com/mirrors/hikaricp?_from=gitee_search
 */
public class HikariSourceDemo {

    private static final Properties PROP = new Properties();

    static {
        try {
            PROP.load(HikariSourceDemo.class.getClassLoader().getResourceAsStream("hikari.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HikariConfig config = new HikariConfig(PROP);
        HikariDataSource ds = new HikariDataSource(config);
        System.out.println(ds.getJdbcUrl());
        System.out.println(ds.getMaximumPoolSize());
        System.out.println(ds.getMinimumIdle());
        Connection conn = null;
        try {
            conn = ds.getConnection();
            System.out.println(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
